package com.example.easybuy.service;

import com.example.easybuy.dao.IndexDao;
import com.example.easybuy.pojo.*;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.apache.jute.Index;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 左边商品菜单
 */
@Service
public class ProductcategoryServiceImpl implements ProductcategoryService{
    private Logger logger= Logger.getLogger(ProductcategoryServiceImpl.class);

    @Autowired
    private IndexDao indexDao;


    /**
     * 单独的查询所有分类
     * @param type
     * @return
     */
    public List<Productcategory> selectProClass(String type){
        logger.debug("ProductcategoryServiceImpl selectProClass type: "+type);
        List<Productcategory> productcategoryList=indexDao.proListOne(type);
        logger.debug("ProductcategoryServiceImpl selectProClass type: "+type+" productcategoryList: "+productcategoryList);
        return productcategoryList;
    }




    /**
     * 查询所有一级菜单的商品
     * @param type
     * @return
     */
    public List<Productcategory> proListOne(String type) {
        logger.debug("ProductcategoryServiceImpl proListOne type: "+type);

        //每个一级分类下的所有商品
        List<Productcategory> productslst=new ArrayList<Productcategory>();

        //查询所有一级分类
        List<Productcategory> productcategoryList=indexDao.proListOne(type);
        for (Productcategory pro1:productcategoryList){
            //是一级分类
                if(pro1.getType()==1){
//                    查出所有的商品下面好一一作比较
                    List<Products> productsList= indexDao.goodsViewList(0);
                    logger.debug("ProductcategoryServiceImpl proListOne productsList: "+productsList);

                    //分类的对象里要有值
                    Productcategory productcategory=pro1;
                    for (Products pro:productsList) {
                        //拿商品表里的一级分类id和一级分类的id比较
                        if (pro1.getId() == pro.getClassifyone()) {
                            //如果相等就通过商品表里的一级分类id去查询一级分类下面的所有商品
                            List<Products> goodsList= indexDao.goodsViewList(pro.getClassifyone());
                            logger.debug("ProductcategoryServiceImpl proListOne goodsList: "+goodsList+" classifyone :"+pro.getClassifyone());
//
//                            把商品存在分类的属性list里一并带到前台拿出来
                            productcategory.setOneClassGoods(goodsList);
                            logger.debug("ProductcategoryServiceImpl proListOne result: "+productcategory );
                        }
                    }
                    //把商品存在自己新建的一个list里
                    productslst.add(productcategory);
                    logger.debug("ProductcategoryServiceImpl proListOne type: "+type+" productcategory: "+productcategory);
                }
        }
        logger.debug("ProductcategoryServiceImpl proListOne type: "+type+" productslst: "+productslst);
        return productslst;
    }

    /**
     * 查询所有商品分类
     */
    public List<Productcategory> proListView() {
        logger.debug("ProductcategoryServiceImpl proListOne");
        List<Productcategory> productcategoryList=indexDao.proListView();
        logger.debug("ProductcategoryServiceImpl proListOne productcategoryList: "+productcategoryList);
        return productcategoryList;
    }

    /*
    查询123级分类
     */
    @Override
    public List<Productcategory> getProductcategoryClass(String type) {
        logger.debug("ProductcategoryServiceImpl getProductcategoryClass()-type:"+type);
        List<Productcategory> productClassList=indexDao.proListOne(type);
        logger.debug("ProductcategoryServiceImpl getProductcategoryClass()-productClassList:"+productClassList);
        return productClassList;
    }

    /*
    分页查询商品分类
     */
    @Override
    public List<Productcategory> getPageProducategory(Page page) {
        logger.debug("ProductcategoryServiceImpl getPageProducategory()-page:"+page);
        //查询数据总条数
        int totalDateCount=indexDao.getProClassCount();
        //页面容量
        page.setPageSize(5);
        //总数据条数
        page.setTotalDataCount(totalDateCount);
        //从几条开始显示=（当前页码-1）*页面容量
        int pageNo=(page.getCurrentPageNo()-1)*page.getPageSize();
        page.setCurrentPageNo(pageNo);

        List<Productcategory> proClassList=indexDao.getPageProducategory(page);
        logger.debug("ProductcategoryServiceImpl getPageProducategory()-proClassList："+proClassList);
        return proClassList;
    }

    /*
    删除分类
     */
    @Override
    public String deletClassById(String id) {
        logger.debug("ProductcategoryServiceImpl deletClassById()-id:"+id);
        int proId=Integer.valueOf(id);
        int deletRow=indexDao.deletClassById(proId);
        String success=null;
        if (deletRow>0){
            success="删除成功";
        }else {
            success="删除失败";
        }
        logger.debug("ProductcategoryServiceImpl deletClassById()-success:"+success);
        return success;
    }

    /*
    查询父级目录
     */
    @Override
    public List getParent() {
        logger.debug("ProductcategoryServiceImpl getParent()...start...");
        List paraentList=indexDao.getParent();
        logger.debug("ProductcategoryServiceImpl getParent()-paraentList:"+paraentList);
        return paraentList;
    }

    /*
    添加分类商品
     */
    @Override
    public String AddProClass(Productcategory productcategory) {
        logger.debug("ProductcategoryServiceImpl AddProClass()-productcategory:"+productcategory);
        int addRow=indexDao.AddProClass(productcategory);
        String success=null;
        if (addRow>0){
            success="添加成功";
        }else {
            success="添加失败";
        }
        logger.debug("ProductcategoryServiceImpl AddProClass()-success:"+success);
        return success;
    }

    /*
    修改分类商品
     */
    @Override
    public String updataPro(Productcategory productcategory) {
        logger.debug("ProductcategoryServiceImpl updataPro()-productcategory:"+productcategory);
        int updataRow=indexDao.updataPro(productcategory);
        String success=null;
        if (updataRow>0){
            success="修改成功";
        }else {
            success="修改失败";
        }
        logger.debug("ProductcategoryServiceImpl updataPro()-success:"+success);
        return success;
    }

    /*
    根据id查询分类
     */
    @Override
    public Productcategory getProClassById(String id) {
        logger.debug("ProductcategoryServiceImpl getProClassById()-id:"+id);
        int getId=Integer.valueOf(id);
        Productcategory productcategory=indexDao.getProClassById(getId);
        logger.debug("ProductcategoryServiceImpl getProClassById()-productcategory:"+productcategory);
        return productcategory;
    }

    /*
    删除分类的时候判断是否存在商品
     */
    @Override
    public List<ProductsManage> getClassIsProducts(String id) {
        logger.debug("ProductcategoryServiceImpl getClassIsProducts()-id:"+id);
        int idPro=Integer.valueOf(id);
        List<ProductsManage> proList=indexDao.getClassIsProducts(idPro);
        logger.debug("ProductcategoryServiceImpl getClassIsProducts()-proList:"+proList);
        return proList;
    }


}
